<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Cart as ModelCart;

/**
 * 购物车接口
 */
class Cart extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];

    /**
     * @Author LK
     * @ApiTitle (添加/编辑购物车)
     * @ApiMethod (POST)
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品ID")
     * @ApiParams (name="product_id", type="integer", required=true, sample="", description="规格id")
     * @ApiParams (name="num", type="integer", required=true, sample="", description="数量")
     * @ApiParams (name="tab", type="string", required=true, sample="", description="1=正常商品,2=积分商品")
     * @ApiParams (name="add_sub", type="string", required=true, sample="", description="0-编辑；1-增加；2-减少")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function add_cart()
    {
        $param = request()->param();
        extract($param);
        $param['user_id'] = $this->auth->id;
        try {
            ModelCart::add($param, $tab, $add_sub, 1);
        } catch (\Exception $e) {
            $this->error('失败:' . $e->getMessage());
        }
        $this->success('成功');
    }

    /**
     * @Author LK
     * @ApiTitle (购物车列表)
     * @ApiMethod (GET)
     * @ApiParams (name="tab", type="int", required=true, sample="", description="1=正常商品,2=积分商品")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function cart_list()
    {
        $param = request()->param();
        extract($param);
        $model = new ModelCart;
        if ($tab == 1) {
            $model->with(['product', 'goods']);
        } else {
            $model->with(['integralgoods']);
        }
        $lists = $model->where('tab', $tab)->where('user_id', $this->auth->id)->where('cart.type', '1')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (购物车总数)
     * @ApiMethod (GET)
     * @ApiParams (name="tab", type="int", required=true, sample="", description="1=正常商品,2=积分商品")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function cart_sum()
    {
        $param = request()->param();
        extract($param);
        $num = ModelCart::where('tab', $tab)->where('user_id', $this->auth->id)->where('type', '1')->sum('num');
        $this->success('', $num);
    }

    /**
     * @Author LK
     * @ApiTitle (删除购物车)
     * @ApiMethod (POST)
     * @ApiParams (name="ids", type="integer", required=true, sample="", description="购物车ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function del_cart()
    {
        $param = request()->param();
        extract($param);
        $res = ModelCart::whereIn('id', $ids)->delete();
        if ($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}
